package com.youxin.logcenter.controller;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.BaseLogModel;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.log.RequestLogBean;
import com.youxin.busfoundation.enums.SystemType;
import com.youxin.foundation.utils.DateUtil;
import com.youxin.foundation.utils.StringUtils;
import com.youxin.logcenter.bean.RequestQueryLog;
import com.youxin.logcenter.common.LogConsts;
import com.youxin.logcenter.model.Logcp;
import com.youxin.logcenter.model.Logoa;
import com.youxin.logcenter.model.Logpc;
import com.youxin.logcenter.service.LogApiService;

/**
 * 日志输出接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class LogApiController {
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LogApiController.class);
	@Autowired
	private LogApiService logApiService;

	@RequestMapping("/log")
	public String testLog(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
		return "log";
	}

	@RequestMapping(value = "/query", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject queryLog(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestQueryLog logBean) {
		try {
			RequestLogBean requestLogBean = new RequestLogBean();
			requestLogBean.setSystemType(logBean.getSystemType());
			if (logBean.getLog() != null) {
				BeanUtils.copyProperties(logBean.getLog(), requestLogBean);
				saveLog(request, httpServletResponse, requestLogBean);
			}
		} catch (Exception e) {
			logger.error("/query log异常", e);
		}
		try {
			if (logBean.getBeginTime() != 0 && logBean.getEndTime() != 0) {
				if (logBean.getBeginTime() >= logBean.getEndTime()) {
					Date endDate = new Date();
					Date beginDate = DateUtil.addDayDate(endDate, -1 * LogConsts.QUERY_LOG_MAXDAYS);
					logBean.setBeginDate(beginDate);
					logBean.setEndDate(endDate);
				} else {
					logBean.setBeginDate(new Date(logBean.getBeginTime()));
					logBean.setEndDate(new Date(logBean.getEndTime()));
				}
			} else if (logBean.getBeginTime() != 0) {
				logBean.setBeginDate(new Date(logBean.getBeginTime()));
				logBean.setEndDate(new Date());
			} else if (logBean.getEndTime() != 0) {
				Date endDate = new Date(logBean.getEndTime());
				Date beginDate = DateUtil.addDayDate(endDate, -1 * LogConsts.QUERY_LOG_MAXDAYS);
				logBean.setBeginDate(beginDate);
				logBean.setEndDate(endDate);
			} else {
				Date endDate = new Date();
				Date beginDate = DateUtil.addDayDate(endDate, -1 * LogConsts.QUERY_LOG_MAXDAYS);
				logBean.setBeginDate(beginDate);
				logBean.setEndDate(endDate);
			}		
			
			return logApiService.queryLog(logBean);
		} catch (Exception e) {
			logger.error("/query异常", e);
			return ResponseObject.systemError(e);
		}
	}

	/**
	 * pc端业务日志
	 * 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param logBean
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject saveLog(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLogBean logBean) {
		try {
			if (logBean == null) {
				return ResponseObject.userFailure("日志参数为空");
			}
			SystemType systemType = SystemType.valueOf(logBean.getSystemType());
			if (systemType == null) {
				return ResponseObject.userFailure("日志参数业务系统类型为空");
			}
			if (StringUtils.isNullOrEmpty(logBean.getIdentifier())) {
				return ResponseObject.userFailure("日志参数标识为空");
			}
			BaseLogModel model = new BaseLogModel();
			BeanUtils.copyProperties(logBean, model);
			return logApiService.saveLog(request, model, systemType);
		} catch (Exception e) {
			logger.error("/disk异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@Async
	@RequestMapping(value = "/saveasync", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Future<ResponseObject> saveLogAsync(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLogBean logBean) {
		ResponseObject result=saveLog(request, httpServletResponse, logBean);
		return new AsyncResult<ResponseObject>(result);
	}
}
